package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.queryObject.RegionQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.LogicException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private DestinationRepository destinationRepository;

    @Override
    public void save(Region region) {
        // 以防万一把要保存的对象的id设置为空
        region.setId(null);
        regionRepository.save(region);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public Region get(String id) {
        Optional<Region> region = regionRepository.findById(id);
        return region.orElse(null);
    }

    @Override
    public List<Region> list() {
        List<Region> all = regionRepository.findAll();
        return all;
    }

    @Override
    public Page<Region> query(RegionQueryObject qo) {

        // 封装查询条件
        Query query = new Query();

        // 总条数
        long totalCount = template.count(query, Region.class);

        // 如果总条数为0 , 那么返回空的分页对象
        if (totalCount==0){
            return Page.empty();
        }

        // 分页对象 , 参数1为当前页 从0开始 , 参数2为每页显示条数 , 参数3为排序方式, 参数4为按哪个字段排序
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.ASC,"id");

        query.with(pageable);// 添加查询条件

        List<Region> list = template.find(query, Region.class);

        // PageImpl 为 Page 接口的一个实现类
        return new PageImpl<>(list, pageable,totalCount );
    }

    /**
     * 查区域中的包含的目的地
     * @param rid   要查的区域的id
     * @return  包含的目的地的集合
     */
    @Override
    public List<Destination> getDestByRegionId(String rid) {
        Region region = regionRepository.findById(rid).orElse(null);
        List<String> refIds = region.getRefIds();
        List<Destination> list = new ArrayList<>();
        for (String refId : refIds) {
            Destination destination = destinationRepository.findById(refId).orElse(null);
            list.add(destination);
        }
        return list;
    }


    /**
     * 修改某个区域的热门状态
     * @param hot   热门状态值
     * @param id    要修改的区域的id
     */
    @Override
    public void changeHotValue(int hot, String id) {
        Region region = regionRepository.findById(id).orElse(null);
        if (region!=null){
            region.setIshot(hot);
            regionRepository.save(region);
        }
    }

    /**
     * 查询状态是热门的,按照序号排列
     * @return
     */
    @Override
    public List<Region> hotRegion() {

        List<Region> regions = regionRepository.findByIshotOrderBySequence(Region.STATE_HOT);

        return regions;
    }

}
